$(function () {

    var edit_dialogItem =  $('#edit_dialogItem');
    var edit_formItem =  $('#edit_formItem');
    var dictionaryItem_datagrid =  $('#dictionaryItem_datagrid');

    //数据表格
    dictionaryItem_datagrid.datagrid({
        url:'/dictionaryItem/list.do',
        title:"字典明细",
        fit:true,
        fitColumns:true,
        striped:true,
        singleSelect:true,
        rownumbers:true,
        toolbar:"#tbItem",
        columns:[[
            {field:'id',hidden:true,width:100},
            {field:'name',title:'字典明细名称',width:100},
            {field:'intro',title:'字典明细介绍',width:100}
        ]]
    });
    //end


    var cmdObj = {
        //添加按钮添加字典明细
        addItem : function () {

            edit_dialogItem.dialog("setTitle","添加字典明细");

            edit_formItem.form("clear");

            edit_dialogItem.dialog("open");
        },
        //编辑按钮编辑字典明细
        editItem : function () {

            var row = dictionaryItem_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的字典明细","info");
                return;
            }

            edit_dialogItem.dialog("setTitle","编辑字典明细");

            console.log(row);

            row["parent.id"] = row.parent.id;

            //在读取数据之前,清除缓存
            edit_formItem.form("clear");

            edit_formItem.form("load",row);

            edit_dialogItem.dialog("open");
        },

        //save按钮提交表单
        saveItem : function () {

            var url = "";

            var id = $("#hiddenItemId").val();

            if(id){
                url = "/dictionaryItem/update.do";
            }else{
                url = "/dictionaryItem/save.do";
            }


            edit_formItem.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialogItem.dialog("close");
                            dictionaryItem_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialogItem.dialog("close");
        },
        //删除字典明细
        removeItem : function () {
            var row = dictionaryItem_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的字典明细","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/dictionaryItem/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                dictionaryItem_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //刷新
        reloadItem :function () {
            dictionaryItem_datagrid.datagrid("reload");
        }

    }

    //抽取优化
    $(".dictionaryItem").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end


    //dialog弹窗
    $('#edit_dialogItem').dialog({
        title: 'My Dialog',
        buttons:"#bbItem",
        closed:true,
        width: 350,
        height: 400,
    });
    //end

})//end